<?php

/* PHP class for downloading CSV files.
Version 1.0.1
Copyright 2005-2007 by Tom Muck
License agreement at http://www.tom-muck.com/extensions/license.cfm
*/

class CSVFile {

	function CSVFile($rs, $fields, $quotes=false, $filename="Download.csv", $totalFields=array(), $totalFieldsHeader=array()){
		ob_start();
		$this->setRs($rs);
		$this->setFields($fields);
		$this->setQuotes($quotes);
		$this->setFilename($filename);
		$this->dumpFile($totalFields, $totalFieldsHeader);
	}
	function setRs($rs){
		$this->_rs = $rs;
	}
	function setQuotes($quotes){
		if($quotes)
			$this->_quotes = '"';
		else
			$this->_quotes = "";
	}
	function setFilename($filename){
		$this->_filename = $filename;
	}
	function setFields($fields){
		$this->_fields = $fields;
	}
	function dumpFile($totalFields, $totalFieldsHeader) {
		mysql_data_seek($this->_rs, 0);
		$lastfield = "";
		ob_end_clean();
//		header("Pragma: public");
//		header("Expires: 0");
//		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//		header("Cache-Control: public");
//		header("Content-Description: File Transfer");
//		header("Content-Type: application/unknown");
//		header("Content-Disposition: filename=" . $this->_filename . "\r\n");
//		header("Content-Transfer-Encoding: binary");
		header("Content-type: application/vnd.ms-excel;charset=UTF-8" );
		header("Content-Disposition: attachment; filename=$this->_filename");
		header("Content-Description: PHP4 Generated Data" );

		$total = array();
		if(0 < count($totalFields))
		{
			$end = count($totalFields);
		}


		if ($row = mysql_fetch_assoc($this->_rs)) {
			if($this->_quotes != "") {
				$allfields = $this->_quotes . implode('","',str_replace('"','""',array_values($this->_fields))) . $this->_quotes;
			}else{
				$allfields = implode(',',array_values($this->_fields));
			}
			echo(mb_convert_encoding($allfields, "Shift-Jis","auto") . "\r\n");

    		do {

				if($this->_quotes != "") {
					$allfields = $this->_quotes . implode('","',$row) . $this->_quotes;
				}else{
					$allfields = implode(',',$row);
				}

				if(0 < $end)
				{
					for($i = 0; $i < $end; $i++)
					{
						$total[$i] += str_replace(",", "", $row[$totalFields[$i]]);
					}
				}

				echo(mb_convert_encoding($allfields, "Shift-Jis","auto") . "\r\n");

    		} while ($row = mysql_fetch_assoc($this->_rs));



			// total header
			$isTotalHeader = false;
			if(0 < count($totalFieldsHeader))
			{
				if($this->_quotes != "") {
					$allfields = $this->_quotes . implode('","',str_replace('"','""',array_values($totalFieldsHeader))) . $this->_quotes;
				}else{
					$allfields = implode(',',array_values($totalFieldsHeader));
				}
				echo(mb_convert_encoding($allfields, "Shift-Jis","auto"));

				$isTotalHeader = true;
			}


			// total
			$end = count($total);
			if(0 < $end)
			{
				for($i = 0; $i < $end; $i++)
				{
					$total[$i] = number_format($total[$i]);
				}

				if($this->_quotes != "") {
					$allfields = $this->_quotes . implode('","',str_replace('"','""',array_values($total))) . $this->_quotes;
				}else{
					$allfields = implode(',',array_values($total));
				}


				if(true === $isTotalHeader)
				{
					echo(",");
				}
				echo(mb_convert_encoding($allfields, "Shift-Jis","auto"));
			}
		}

		exit();
	}
}


?>
